package br.ufpe.cin.tose.copapolling;

import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.util.Log;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;

/**
 * Created by salc on 11/17/13.
 */
class GetHTML extends AsyncTask<String, Void, String> {


	public String URLTimes = "http://www.cin.ufpe.br/~salc/times";
	public String URLJogos = "http://www.cin.ufpe.br/~salc/jogos";
	int indexSharedPreferencesTimes = 0;
	int indexSharedPreferencesJogos = 0;
	
    @Override
    protected String doInBackground(String... urls) {

    	URLTimes = urls[0];
    	URLJogos = urls[1];
    	
    	String times = downloadTimes();
    	String jogos = downloadJogos();
		
		return times + "!" + jogos;
    }

    @Override
    protected void onPostExecute(String output) {
    	
    	String[] dados = output.split("!");
    	AtualizaService.atualizaService.retornoDoWork(dados[0], dados[1]);
    	try {
			Thread.sleep(30000);
		} catch (InterruptedException e) {
			Log.d("serviceatualiza", e.getMessage());
		}
    	MainActivity.mainActivity.repete();
    }
    
    public String downloadTimes() {
		InputStream stream = null;
		FileOutputStream fos = null;
		String output = "";
		try {
			URL url = new URL(URLTimes);
			char[] charBuffer;
			stream = url.openConnection().getInputStream();
			InputStreamReader reader = new InputStreamReader(stream);
			int next = -1;
			while ((next = reader.read()) != -1) {
				output += (char) next;
			}
		} catch (Exception e) {
			Log.d("serviceatualiza", e.getMessage());
		} finally {
			if (stream != null) {
				try {
					stream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return output;
	}

	public String downloadJogos() {
		InputStream stream = null;
		FileOutputStream fos = null;
		String output = "";
		
		try {
			URL url = new URL(URLJogos);

			SharedPreferences keyValues = PreferenceManager
					.getDefaultSharedPreferences(MainActivity.mainActivity.getApplicationContext());
			SharedPreferences.Editor keyValuesEditor = keyValues.edit();

			char[] charBuffer;
			stream = url.openConnection().getInputStream();
			InputStreamReader reader = new InputStreamReader(stream);
			int next = -1;
			while ((next = reader.read()) != -1) {
				output += (char) next;
			}
		} catch (Exception e) {
			Log.d("serviceatualiza", e.getMessage());
		} finally {
			if (stream != null) {
				try {
					stream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return output;
	}
}
